extends Area2D

@onready var title_node = $PanelContainer/Title
@onready var bg_node = $PanelContainer/Background
@onready var collision_shape = $CollisionShape2D
@onready var container_node = $PanelContainer
@onready var timer1 = $Timer
@onready var timer2 = $Timer2
@onready var line_panel = $Line2D

var sup_panel
var world_panel
var node_data: Dictionary = {}

var child_panel_dic: Dictionary = {
	"bottom":{"pos":Vector2(0,0),"node":null,"data":null}, 
	"bottom_left":{"pos":Vector2(0,0),"node":null,"data":null}, 
	"bottom_right":{"pos":Vector2(0,0),"node":null,"data":null},
	"right":{"pos":Vector2(0,0),"node":null,"data":null},
	"right_top":{"pos":Vector2(0,0),"node":null,"data":null},
	"right_bottom":{"pos":Vector2(0,0),"node":null,"data":null},
	"top":{"pos":Vector2(0,0),"node":null,"data":null}, 
	"top_left":{"pos":Vector2(0,0),"node":null,"data":null}, 
	"top_right":{"pos":Vector2(0,0),"node":null,"data":null},
	"left":{"pos":Vector2(0,0),"node":null,"data":null},
	"left_top":{"pos":Vector2(0,0),"node":null,"data":null},
	"left_bottom":{"pos":Vector2(0,0),"node":null,"data":null}
	}
var child_list: Array = []
var node_type: String = 'def'
var click_num = 0
var click_type: String = ""
var old_pos: Vector2 = Vector2(0,0)
var is_show_child = false

var child_span = 10

var longpressed = false
var doubleclick = false
var connect_obj

func set_def_data(data, world, s_panel):
	world_panel = world
	node_data = data
	
	if s_panel:
		sup_panel = sup_panel
	
	var title: String = data['title']
	title_node.text = title
	if title.length() < 5 :
		container_node.size = Vector2(100, 30)
		#click_node.set_rect(size)
		var rectangle: RectangleShape2D = collision_shape.shape
		rectangle.size = container_node.size
	else :
		container_node.size = Vector2(title.length() * 10 + 70, 30)
		var rectangle: RectangleShape2D = collision_shape.shape
		rectangle.size = container_node.size
		#click_node.set_rect(size)
	
	var child_str: String = data['child_node']
	if child_str.length() > 0:
		child_list = child_str.split("/")
	
	set_def_type()
	

func set_pos(pos: Vector2):
	old_pos = pos
	container_node.position = pos
	position = pos
	collision_shape.position = pos + Vector2(container_node.size.x / 2, container_node.size.y / 2)
	_init_child_data()

func set_def_type():
	node_type = 'def'
	title_node.modulate = Color("#FFFFFF")
	var new_stylebox_normal = bg_node.get_theme_stylebox("panel").duplicate()
	new_stylebox_normal.bg_color = Color("#7D7D7D")
	bg_node.add_theme_stylebox_override("panel", new_stylebox_normal)

func set_sel_type():
	node_type = 'sel'
	var new_stylebox_normal = bg_node.get_theme_stylebox("panel").duplicate()
	new_stylebox_normal.bg_color = Color("#EE0F0F")
	bg_node.add_theme_stylebox_override("panel", new_stylebox_normal)

func set_look_type():
	node_type = 'look'
	title_node.modulate = Color("#000000")

func show_child():
	if child_list.size() > 0:
		if not is_show_child:
			is_show_child = true
			world_panel.emit_signal("show_child_back", self)
	else:
		play_dou_dong_anim()

func _on_timer_timeout():
	if longpressed:
		print("长按")
		send_type('long')
	else:
		doubleclick = false
		print("单击")
		send_type('short')

func _on_input_event(viewport, event, shape_idx):
	if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT and event.pressed:
		timer1.start()
		if timer2.time_left == 0:
			timer2.start()
		else:
			doubleclick = true
			timer2.stop()
			print("双击")
			send_type('double')
		longpressed = true
	if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT and not event.pressed:
		if timer1.time_left > 0:
			longpressed = false
	#if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT and event.doubleclick:
		#print("godot_doubleclick")
	#if event is InputEventMouseButton:
		#print(node_data['id'])

func send_type(type: String):
	click_type = type
	if world_panel:
		world_panel.emit_signal("click_node_back", self)

func play_dou_dong_anim():
	$AnimationPlayer.play("dou_dong")

func _anim_pos_left():
	position.x = old_pos.x + randi_range(1, 6)
	position.y = old_pos.y + randi_range(1, 6)
func _anim_pos_right():
	position.x = old_pos.x - randi_range(1, 6)
	position.y = old_pos.y - randi_range(1, 6)
func _anim_pos_origin():
	position = old_pos

func _init_child_data():
	if sup_panel:
		pass
	else:
		child_panel_dic.bottom.pos = Vector2(position.x, position.y + (child_span + container_node.size.y))
		child_panel_dic.bottom_left.pos = Vector2(position.x - (child_span + container_node.size.x), position.y + (child_span + container_node.size.y))
		child_panel_dic.bottom_right.pos = Vector2(position.x + (child_span + container_node.size.x), position.y + (child_span + container_node.size.y))
		child_panel_dic.right.pos = Vector2(position.x + (child_span + container_node.size.x), position.y)
		child_panel_dic.right_top.pos = Vector2(position.x + (child_span + container_node.size.x), position.y - (child_span + container_node.size.y))
		child_panel_dic.right_bottom.pos = Vector2(position.x + (child_span + container_node.size.x), position.y + (child_span + container_node.size.y))
		child_panel_dic.top.pos = Vector2(position.x, position.y - (child_span + container_node.size.y))
		child_panel_dic.top_left.pos = Vector2(position.x - (child_span + container_node.size.x), position.y - (child_span + container_node.size.y))
		child_panel_dic.top_right.pos = Vector2(position.x + (child_span + container_node.size.x), position.y - (child_span + container_node.size.y))
		child_panel_dic.left.pos = Vector2(position.x - (child_span + container_node.size.x), position.y)
		child_panel_dic.left_top.pos = Vector2(position.x - (child_span + container_node.size.x), position.y - (child_span + container_node.size.y))
		child_panel_dic.left_bottom.pos = Vector2(position.x - (child_span + container_node.size.x), position.y + (child_span + container_node.size.y))

func set_child_data(type, data, panel):
	child_panel_dic[type].node = panel
	child_panel_dic[type].data = data

